/*
 * main.c
 */

/*
 *
 * ---Connections---
 *
 * PA2	CE		output
 * PA3	I/O		input/output - initialise output
 * PA4	SCLK	output
 *
 * PA5	Mode	input		interrupt
 * PA6	time++	input		interrupt
 * PA7	time--	input		interrupt
 *
 * PB0	10h		output
 * PB1
 * PB2
 * PB3
 *
 * PB4	h		output
 * PB5
 * PB6
 * PB7
 *
 * PC4	10m		output
 * PC5
 * PC6
 * PC7
 *
 * PD0	m		output
 * PD1
 * PD2
 * PD3
 *
 * PE0	10s		output
 * PE1
 * PE2
 * PE3
 *
 * (PF0 is NMI)
 * PF1	s		output
 * PF2
 * PF3
 * PF4
 *
 *****************************************************************************************************
 *
 *
 *	74141 notes
 *
 *	BCD -> decimal
 *
 *	Fully mapped outputs. Digit blanking is achieved by leaving all inputs high.
 *	Inputs default to high state, need to be manually pulled low.
 *
 *	74141 Inputs shall be driven by optocouplers.
 *	This means that for an open collector drive pin, the uC pins should be pulled
 *	 HIGH when the digit is off, LOW when the digit is on.
 *
 *
 *****************************************************************************************************
 *
 *	User Pushbuttons
 *
 *	Should be active when pulled high
 *	Generate interrupt/poll on rising edge
 *
 *
 *****************************************************************************************************
 * DS1302 connections:
 * CE - PA2  (Says RST on silkscreen)
 * I/O - PA3 (internal pull down on device)
 * SCK - PA4 (internal pull down on device)
 *
 *
 * DS1302 interface
 * -------------------------------------------------------------
 * Address/command byte
 * 	|	7	|	6	|	5	|	4	|	3	|	2	|	1	|	0	|
 * 	|	1	|RAM/CK	|	A4	|	A3	|	A2	|	A1	|	A0	| RD/WR	|
 *
 *	Send bit0 first
 *
 *****************************************************************************************************
 * 	Single byte read
 *
 * 	      __________________________________________________________________________________
 * 	CE   /                                                                                  \
 * 	____/                                                                                    \_____
 *
 *
 * 	         __   ___   __   __   __   __   __   ___   __   __   __   __   __   __   __
 * 	SCK ____|^ |_|^  |_|^ |_|^ |_|^ |_|^ |_|^ |_|^ v|_| v|_| v|_| v|_| v|_| v|_| v|_| v|___________
 *
 *
 * 	      _______________________________________   _______________________________________
 * 	I/0  /    |    |    |    |    |    |    |    \ /    |    |    |    |    |    |    |    \
 * 	----- R/W | A0 | A1 | A2 | A3 | A4 |R/C |  1  X D0  | D1 | D2 | D3 | D4 | D5 | D6 | D7  -------
 * 	     \____|____|____|____|____|____|____|____/ \____|____|____|____|____|____|____|____/
 *
 *
 *****************************************************************************************************
 * 	Single byte write
 *
 * 	      __________________________________________________________________________________
 * 	CE   /                                                                                  \
 * 	____/                                                                                    \_____
 *
 *
 * 	         __   __   __   __   __   __   __   __   __   __   __   __   __   __   __   __
 * 	SCK ____|^ |_|^ |_|^ |_|^ |_|^ |_|^ |_|^ |_|^ |_|^ |_|^ |_|^ |_|^ |_|^ |_|^ |_|^ |_|^ |________
 *
 *
 * 	      _______________________________________   _______________________________________
 * 	I/0  /    |    |    |    |    |    |    |    \ /    |    |    |    |    |    |    |    \
 * 	----- R/W | A0 | A1 | A2 | A3 | A4 |R/C |  1  X D0  | D1 | D2 | D3 | D4 | D5 | D6 | D7  -------
 * 	     \____|____|____|____|____|____|____|____/ \____|____|____|____|____|____|____|____/
 *
 *
 *****************************************************************************************************
 *
 * 	Bit 7 must be a 1
 * 	Bit 6 specifies clock/calendar data when 0, RAM data when 1
 * 	Bits 5-1 specify whether the designated registers are input or output
 * 	Bit 0 specifies a write operation (input) if logic 0, Read operation (output) if logic 1
 * 	Command byte is always input, starting with the LSB (bit 0)
 *
 * 	Driving CE high initiates a data transfer
 *
 * 	Data is clocked in on the rising edge, clocked out of the falling edge
 * 	SCK must be logic 0 when CE is driven to logic 1
 *
 * 	Data is output starting with bit 0
 *
 *****************************************************************************************************
 *
 *	Table
 *	---------------------------------------------------------------------------------------------
 *	| Read	| Write	| Bit 7	| Bit 6	| Bit 5	| Bit 4	| Bit 3	| Bit 2	| Bit 1	| Bit 0	|	Range	|
 *	| 0x81	| 0x80	|  CH 	|		10 Seconds		|				Seconds			|	00-59	|
 *	| 0x83	| 0x82	|		|		10 minutes 		|				Minutes			|	00-59	|
 *	| 0x85	| 0x84	|12\!24	|	0	|10\!AM/PM| Hour|				Hour			| 1-12/0-23 |
 *	| 0x87	| 0x86	|	0	|	0	|	10 Date		|				Date			|	1-31	|
 *	| 0x89	| 0x88	|	0	|	0	|	0	|10Month|				Month			|	1-12	|
 *	| 0x8B	| 0x8A	|	0	| 	0	|	0	|	0	|	0	|		Day				|	0-7		|
 *	| 0x8D	| 0x8C	|			10 Year				|				Year			|	00-99	|
 *	| 0x8F	| 0x8E	|	WP	|	0	|	0	|	0	|	0	|	0	|	0	|	0	|	---		|
 *	---------------------------------------------------------------------------------------------
 *
 *	Clock burst
 *	-----------------
 *	| 0xBF	| 0xBE	|
 *	-----------------
 *
 */


#define	F_CPU	40000000

#include "stdint.h"
#include "stdbool.h"
#include "driverlib/sysctl.h"
#include "driverlib/gpio.h"
#include "inc/hw_types.h"
#include "inc/tm4c123gh6pge.h"


#define CE			GPIO_PIN_2
#define DATALINE	GPIO_PIN_3
#define SCK			GPIO_PIN_4
#define CLOCKPORT	GPIO_PORTA_DATA_R

#define	SECPORT		GPIO_PORTF_DATA_R
#define T_SECPORT	GPIO_PORTE_DATA_R
#define	MINPORT		GPIO_PORTD_DATA_R
#define	T_MINPORT	GPIO_PORTC_DATA_R
#define	HOURPORT	GPIO_PORTB_DATA_R
#define	T_HOURPORT	GPIO_PORTB_DATA_R


#define BUTTONPORT	GPIO_PORTA_DATA_R
#define	MAX_CHECKS	6	//*5ms = 30ms debounce
unsigned int DebouncedState = 0;
unsigned int prevState = 0;
unsigned int _dbncState[MAX_CHECKS];
unsigned int _dbncIndex = 0;


#define BLANK_COUNT	200
unsigned int BlankDisplay = 0;
unsigned int _blankIndex = 0;


#define DISP_HMS	1
#define DISP_YMD	2
#define	DISP_INTF	3
unsigned int DisplayMode = DISP_HMS;

#define	_5_SEC		1000
unsigned int modeButtonPressTime;
unsigned int incrementButtonPressTime;
unsigned int decrementButtonPressTime;



#define	HIGH		1
#define	LOW			0
#define IN			1
#define	OUT			0
#define HR24		0
#define AM			1
#define PM			2


struct DS1302_Clock{
	unsigned int Second;	//0-59
	unsigned int Minute;	//0-59
	unsigned int Hour;		//0-23
	unsigned int AmPm24hr;	//0->24, 1->am, 2->pm
	unsigned int Day;		//1-31
	unsigned int Month;		//1-12
	unsigned int Year;		//0-99	(2000-2099)
	unsigned int Weekday;	//0-6	(starting Sunday?)
};

struct DS1302_Clock Clock;

unsigned int DataVal = 0, _tempDataIn = 0, _tempDataOut = 0;

 void _delay_ns(unsigned long j){
 	//not even close to accurate, but doesn't really matter for this application
 	// F_CPU 	= 40 000 000 ticks per second
 	// 			= 40 ticks per micro second
 	// if desired delay is 1 us, wait 40 ticks.
 	// this is assuming the i-- operation can be done in one cycle. Which is bollocks.

	 /*LOL - this seriously does nothing.*/
	 unsigned int i=0;
	 j *= F_CPU / 1000;

//	 do{i++;}
//	 while(i<j);

}


// Debounce logic adapted from http://www.ganssle.com/debouncing-pt2.htm
unsigned int RawKeyPressed(){
	// only care about pins 5, 6, 7
	return BUTTONPORT & 0xE0;
}


void DebounceSwitches(){

	unsigned int i,j;
	_dbncState[_dbncIndex++] = RawKeyPressed();
	j=0xE0;
	for(i=0;i<MAX_CHECKS;i++) j &= _dbncState[i];
	DebouncedState = j;
	if(_dbncIndex >= MAX_CHECKS) _dbncIndex = 0;

}


void ButtonAction(){


	//Check rising edge of mode button press
	if((DebouncedState & ~prevState) & 0x20){

	}
	//Check falling edge of mode button press
	else if((~DebouncedState & prevState) & 0x20){

		if(DisplayMode == DISP_HMS){
			DisplayMode = DISP_YMD;
		} else if(DisplayMode == DISP_YMD){
			DisplayMode = DISP_HMS;
		}

	}

	//Check rising edge of increment button press
	if((DebouncedState & ~prevState) & 0x40){

	}
	//Check falling edge of increment button press
	else if((~DebouncedState & prevState) & 0x40){

	}

	//Check rising edge of decrement button press
	if((DebouncedState & ~prevState) & 0x80){

	}
	//Check falling edge of decrement button press
	else if((~DebouncedState & prevState) & 0x80){

	}

	//check that the mode button is being pressed
	if(DebouncedState & 0x20){
		//Button needs to be pressed for 5 seconds to enter programming mode
		if(modeButtonPressTime >= _5_SEC){
			DisplayMode = DISP_INTF;
		}
	}



	prevState = DebouncedState;
}


 void TIMER5_A_INT(void){
	 unsigned long TimerStatus;

	 // Get the interrupt status
	 TimerStatus = WTIMER5_MIS_R;

	 // Clear the asserted interrupts
	 WTIMER5_ICR_R = TimerStatus;

	 DebounceSwitches();

	 //Drive the display blanking feature
	 if(DisplayMode != DISP_INTF){
		 BlankDisplay = LOW;
		 _blankIndex = 0;
	 } else{
		 if(++_blankIndex > BLANK_COUNT){
			 _blankIndex = 0;
			 BlankDisplay ^= HIGH;
		 }
	 }

	 //Count how long buttons have been pressed, in 5ms increments
	 if(DebouncedState & 0x20){
		modeButtonPressTime++;
	 } else {
		 modeButtonPressTime = 0;
	 }

	 if(DebouncedState & 0x40){
		 incrementButtonPressTime++;
	 } else {
		 incrementButtonPressTime = 0;
	 }

	 if(DebouncedState & 0x80){
		decrementButtonPressTime++;
	 } else {
		 decrementButtonPressTime = 0;
	 }
 }



// Timer 5A is set to generate an interrupt every 5ms
 void Timer5_Init(void){


         /**********************/
         /*  Configure TIMER5  */
         /**********************/

         /* Enable Wide TIMER5 peripheral */
         SYSCTL_RCGCWTIMER_R |= SYSCTL_RCGCWTIMER_R5;

         /* Disable Timers A and B */
         WTIMER5_CTL_R &= ~(0x00000101);
         /* 32 bit timer */
         WTIMER5_CFG_R = 0x00000004;
         /* Configure as PWM, continuous, interrupt enabled */
         WTIMER5_TAMR_R = 0x0000020A;

         /* Timer 5A generates on rising edges */
         WTIMER5_CTL_R |= 0x00000000;

         /* Invert the output on Timer5A-PWM */
         WTIMER5_CTL_R |= 0x0000040;

         /* Timer 5A and Timer5B Event Interrupts Enabled */
         WTIMER5_IMR_R = 0x00000004;

         /* 48 bit match value - TAPR contains bits 32-47, TAMATCHR contains bits 0-31 */
         /* Given fCPU 40MHz, need period of 5ms */
         /* 40E6*0.005 = 0d200000 = 0x00030D40*/
         WTIMER5_TAPR_R = 0x0000;
         WTIMER5_TAILR_R = 0x00030D40;


         /* 48 bit match value - TAPMR contains bits 32-47, TAMATCHR contains bits 0-31 */
         /* Given fCPU 40MHz, need high time of 0.5ms */
         /* 40E6*0.5E-3 = 0d20000 = 0x00004E20 */
         WTIMER5_TAPMR_R = 0x0000;
         WTIMER5_TAMATCHR_R = 0x00004E20;


         // Enable Timer5A interrupts in the NVIC
         NVIC_EN3_R |= 0x00000100;


         /* Enable Timer 1A */
         WTIMER5_CTL_R |= 0x00000001;

 }




void GPIO_Init(){

	volatile unsigned long __temp;

	// Enable PORTD peripheral
	SYSCTL_RCGCGPIO_R |= SYSCTL_RCGCGPIO_R0|SYSCTL_RCGCGPIO_R1|SYSCTL_RCGCGPIO_R2|SYSCTL_RCGCGPIO_R3|SYSCTL_RCGCGPIO_R4|SYSCTL_RCGCGPIO_R5;				//Add PORTS A-F to enabled peripherals

    __temp = SYSCTL_RCGCGPIO_R;


	GPIO_PORTA_AFSEL_R = 0x00;
	GPIO_PORTA_PCTL_R = 0x00;
	GPIO_PORTB_AFSEL_R = 0x00;
	GPIO_PORTB_PCTL_R = 0x00;
	GPIO_PORTC_AFSEL_R &= 0x00;
	GPIO_PORTC_PCTL_R &= 0xFFFFFFFF;
	GPIO_PORTD_AFSEL_R = 0x00;
	GPIO_PORTD_PCTL_R = 0x00;
	GPIO_PORTE_AFSEL_R = 0x00;
	GPIO_PORTE_PCTL_R = 0x00;
	GPIO_PORTF_AFSEL_R = 0x00;
	GPIO_PORTF_PCTL_R = 0x00;


	// Configure PORTS. See header for configuration
	GPIO_PORTA_DIR_R = GPIO_PIN_2|GPIO_PIN_3|GPIO_PIN_4;
	GPIO_PORTB_DIR_R = GPIO_PIN_0|GPIO_PIN_1|GPIO_PIN_2|GPIO_PIN_3|GPIO_PIN_4|GPIO_PIN_5|GPIO_PIN_6|GPIO_PIN_7;
	GPIO_PORTC_DIR_R = GPIO_PIN_4|GPIO_PIN_5|GPIO_PIN_6|GPIO_PIN_7;
	GPIO_PORTD_DIR_R = GPIO_PIN_0|GPIO_PIN_1|GPIO_PIN_2|GPIO_PIN_3;
	GPIO_PORTE_DIR_R = GPIO_PIN_0|GPIO_PIN_1|GPIO_PIN_2|GPIO_PIN_3;
	GPIO_PORTF_DIR_R = GPIO_PIN_1|GPIO_PIN_2|GPIO_PIN_3|GPIO_PIN_4;

	GPIO_PORTA_DEN_R = GPIO_PIN_2|GPIO_PIN_3|GPIO_PIN_4;
	GPIO_PORTB_DEN_R = GPIO_PIN_0|GPIO_PIN_1|GPIO_PIN_2|GPIO_PIN_3|GPIO_PIN_4|GPIO_PIN_5|GPIO_PIN_6|GPIO_PIN_7;
	GPIO_PORTC_DEN_R = GPIO_PIN_4|GPIO_PIN_5|GPIO_PIN_6|GPIO_PIN_7;
	GPIO_PORTD_DEN_R = GPIO_PIN_0|GPIO_PIN_1|GPIO_PIN_2|GPIO_PIN_3;
	GPIO_PORTE_DEN_R = GPIO_PIN_0|GPIO_PIN_1|GPIO_PIN_2|GPIO_PIN_3;
	GPIO_PORTF_DEN_R = GPIO_PIN_1|GPIO_PIN_2|GPIO_PIN_3|GPIO_PIN_4;

	GPIO_PORTA_ODR_R = 0;
	GPIO_PORTB_ODR_R = 0;
	GPIO_PORTC_ODR_R = 0;
	GPIO_PORTD_ODR_R = 0;
	GPIO_PORTE_ODR_R = 0;
	GPIO_PORTF_ODR_R = 0;

	GPIO_PORTA_DATA_R = 0;
	GPIO_PORTB_DATA_R = 0;
	GPIO_PORTC_DATA_R = 0;
	GPIO_PORTD_DATA_R = 0;
	GPIO_PORTE_DATA_R = 0;
	GPIO_PORTF_DATA_R = 0;

	GPIO_PORTA_PUR_R = GPIO_PIN_5|GPIO_PIN_6|GPIO_PIN_7;

}

void Data(unsigned int bit, unsigned int direction){

	// data being read in from clock
	if(direction == IN){
		_tempDataIn |= (((CLOCKPORT & DATALINE) >> 3) << bit);

	// data pushed out to clock
	}else{
		unsigned int bitWrite = ((_tempDataOut & (0x01 << bit)) > 0) ? 1 : 0;
		CLOCKPORT = (CLOCKPORT & ~DATALINE) | bitWrite << 3;
	}
}

void AssertCE(unsigned int Dir){

	if(Dir == HIGH){
		CLOCKPORT |= CE;
	}else{
		CLOCKPORT &= ~CE;
	}
}

void AssertSCK(unsigned int Dir){

	if(Dir == HIGH){
		CLOCKPORT |= SCK;
	}else{
		CLOCKPORT &= ~SCK;
	}

}


void ReadClock(unsigned int address){

	unsigned int i;
	_tempDataOut = address;

	//Dataline is an output
	GPIO_PORTA_DIR_R |= DATALINE;

	//Ensure SCK is low first
	AssertSCK(LOW);

	//Assert CE
	AssertCE(HIGH);

	_delay_ns(500);

	//Clock out one byte		//
	for(i=0;i<7;i++){			//
		Data(i, OUT);			//
		_delay_ns(500);			//
		AssertSCK(HIGH);		//
		_delay_ns(1000);		//
		AssertSCK(LOW);			//
		_delay_ns(500);			//
	}							//
	Data(7, OUT);				//
	_delay_ns(500);				//
	AssertSCK(HIGH);			//

	//Change dataline to input		//
	_delay_ns(1000);				//
	GPIO_PORTA_DIR_R &= ~DATALINE;	//
	_delay_ns(1000);				//

	//Initialise Data In variable
	_tempDataIn = 0;

	//Clock in one byte			//
	for(i=0;i<7;i++){			//
		AssertSCK(LOW);			//
		_delay_ns(500);			//
		Data(i,IN);				//
		_delay_ns(500);			//
		AssertSCK(HIGH);		//
		_delay_ns(1000);		//
	}							//
	AssertSCK(LOW);				//
	_delay_ns(1000);			//
	Data(7,IN);					//


	_delay_ns(500);

	//Deassert CE
	AssertCE(LOW);

	DataVal = _tempDataIn;

}


void WriteClock(unsigned int address, unsigned int writeByte){

		unsigned int i;
		_tempDataOut = address;

		//Dataline is an output
		GPIO_PORTA_DIR_R |= DATALINE;

		//Ensure SCK is low first
		AssertSCK(LOW);

		//Assert CE
		AssertCE(HIGH);

		_delay_ns(500);

		//Clock out one byte		//
		for(i=0;i<8;i++){			//
			Data(i, OUT);			//
			_delay_ns(500);			//
			AssertSCK(HIGH);		//
			_delay_ns(1000);		//
			AssertSCK(LOW);			//
			_delay_ns(500);			//
		}							//

		_tempDataOut = writeByte;

		//Clock out one byte		//
		for(i=0;i<8;i++){			//
			Data(i, OUT);			//
			_delay_ns(500);			//
			AssertSCK(HIGH);		//
			_delay_ns(1000);		//
			AssertSCK(LOW);			//
			_delay_ns(500);			//
		}							//


		_delay_ns(500);

		//Deassert CE
		AssertCE(LOW);

		DataVal = _tempDataIn;
}

void GetTime(){
	//Get Seconds
	ReadClock(0x81);
	Clock.Second = (DataVal & 0x0F) + (10*((DataVal & 0x70) >> 4));

	//Get Minutes
	ReadClock(0x83);
	Clock.Minute = (DataVal & 0x0F) + (10*((DataVal & 0x70) >> 4));

	//Get Hour
	ReadClock(0x85);

	//clock is in 12hr mode
	if(DataVal & 0x80){
		Clock.AmPm24hr = DataVal & 0x20 ? PM : AM;
		Clock.Hour = (DataVal & 0x0F) + (10*((DataVal & 0x10)) >> 4);
	//clock is in 24hr mode
	}else{
		Clock.AmPm24hr = HR24;
		Clock.Hour = (DataVal & 0x0F) + (10*((DataVal & 0x30)) >> 4);
	}

	//Get Day
	ReadClock(0x87);
	Clock.Day = (DataVal & 0x0F) + (10*((DataVal & 0x30)) >> 4);

	//Get Month
	ReadClock(0x89);
	Clock.Month = (DataVal & 0x0F) + (10*((DataVal & 0x10)) >> 4);

	//Get Day Of Week
	ReadClock(0x8B);
	Clock.Weekday = (DataVal & 0x07);

	//Get Year
	ReadClock(0x8D);
	Clock.Year = (DataVal & 0x0F) + (10*((DataVal & 0xF0) >> 4));
}


unsigned int EncodeBCD(unsigned int input, unsigned int bits){
	unsigned int _temp = 0, lowerNibble = 0, upperNibble = 0;

	bits = bits > 8 ? 8 : bits;

	lowerNibble = input % 10;
	upperNibble = (input-lowerNibble)/10;
	_temp = lowerNibble + (upperNibble << 4);

	//mask out the bits we don't want
	_temp &= 0xFF >> (8-bits);

	return _temp;
}

void SetTimeInterface(){
	unsigned int _hold = 1;

	while(_hold){

	}
}

void SetTime(){
	unsigned int _temp = 0;

	//Set Seconds
	_temp = EncodeBCD(Clock.Second, 7);
	WriteClock(0x80,_temp);

	//Set Minutes
	_temp = EncodeBCD(Clock.Minute, 7);
	WriteClock(0x82,_temp);

	//Set Hour
	//clock is in 12hr mode
	if(Clock.AmPm24hr != HR24){
		_temp = EncodeBCD(Clock.Hour, 5);
		if(Clock.AmPm24hr == PM){
			//set PM bit
			_temp |= 0x20;
		}

		//set 12hr bit
		_temp |= 0x80;
	//clock is in 24hr mode
	}else{

		_temp = EncodeBCD(Clock.Hour, 6);
	}
	WriteClock(0x84,_temp);


	//Set Day
	_temp = EncodeBCD(Clock.Day, 6);
	WriteClock(0x86,_temp);

	//Set Month
	_temp = EncodeBCD(Clock.Month, 5);
	WriteClock(0x88,_temp);

	//Set Day Of Week
	_temp = EncodeBCD(Clock.Weekday, 3);
	WriteClock(0x8A,_temp);

	//Set Year
	_temp = EncodeBCD(Clock.Year, 8);
	WriteClock(0x8C,_temp);
}


void NixieDisplay(void){
	unsigned int _temp, lowerNibble = 0, upperNibble = 0;

	if(DisplayMode == DISP_HMS){

		//bitwise not the _tempword, because thats how the opto-isolators are configured

		_temp = EncodeBCD(Clock.Second, 7);
		lowerNibble = (~_temp & 0x0F);
		upperNibble = (~_temp & 0xF0) >> 4;
		SECPORT = (SECPORT & 0xE1) | (lowerNibble << 1);
		T_SECPORT = (T_SECPORT & 0xF0) | upperNibble;

		_temp = EncodeBCD(Clock.Minute, 7);
		lowerNibble = (~_temp & 0x0F);
		upperNibble = (~_temp & 0xF0);
		MINPORT = (MINPORT & 0xF0) | lowerNibble;
		T_MINPORT = (T_MINPORT & 0x0F) | upperNibble;

		_temp = EncodeBCD(Clock.Hour, 7);
		lowerNibble = (~_temp & 0x0F);
		upperNibble = (~_temp & 0xF0) >> 4;
		HOURPORT = (HOURPORT & 0x0F) | (lowerNibble << 4);
		T_HOURPORT = (T_HOURPORT & 0xF0) | upperNibble;
	}



}




void main(void){

	volatile unsigned long __temp, foo, i;

	unsigned int setTimeRequest = 0;

    //Setup the system clock - 40MHz
    SysCtlClockSet(SYSCTL_SYSDIV_5|SYSCTL_USE_PLL|SYSCTL_XTAL_16MHZ|SYSCTL_OSC_MAIN);

    GPIO_Init();
    Timer5_Init();


	while(1){


		if(setTimeRequest){
			setTimeRequest = 0;
			SetTimeInterface();
			SetTime();
		}else{
			GetTime();
		}

		//Update Display
		NixieDisplay();

	}
}
